<?php
class category_admin extends MX_Controller{
	public $category;
	public function index(){
		$this->model('category_models_index');
		$list_option=$this->category_models_index->get_list_option();
		if(isset($_POST)){
			foreach ($_POST as $key=>$value)
				if(!is_array($value))$data[$key]=trim($value);
				else $data[$key]=$value;
				if(isset($data["option"])) $data["option"]=implode(',',$data["option"]);
				
				$data["showPrice"]=isset($data["showPrice"])?1:0;
		}
		if(isset($_POST['add_category'])){
			unset($data['add_category']);
			$this->category_models_index->add_categories($data);
		}
		if(isset($_POST['Update_Category'])){
			unset($data['Update_Category']);
			$this->category_models_index->update_categories($data);
		}
		$filter='';

		if(isset($_POST['search_categories'])){
			$filter=$_POST['search_categories'];
		}
		
		$adminMenu= $this->load->module('adminMenu/index');
		
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		
		$option_category=self::wp_dropdown_cats();
		$this->load->helper('form');
		$tableCategoryHeader=array(0=>null,'name'=>'Nom','slug'=>'URL parent','option'=>'Option','count'=>'Poster');
		self::listTableCategory( 0,  0, 0,$filter);
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'option_category'=>$option_category,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'tableCategoryHeader'=>$tableCategoryHeader,
					'tableCategoryBody'=>$this->category,
					'CategoryListOption'=>$list_option
				);
		
		
		$this->view(
				array('admin-header',
						'category',
						'admin-footer'), 
				$data); // Display the page
	}
	public function delete($id){
		$this->model('category_models_index');
		$check_subCategory=$this->category_models_index->get_categories($id);
		
		if(count($check_subCategory)==0){
			$this->category_models_index->delete_categories($id);
		}else{
			foreach ($check_subCategory as $subCategory)
				$subCatName[]=$subCategory->name;
			$_SESSION['notification'][]="Ce Categorie n'est pas vide / supprimer sous-categorie (".implode(', ',$subCatName).")";
		}
		$this->load->helper('url');
		redirect('category/admin');
	}

	public function edit($id){

		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		
		$category=$this->model('category_models_index')->get_category($id);
		$list_option=$this->category_models_index->get_list_option($id);
		$adminMenu= $this->load->module('adminMenu/index');
		
		$option_category=self::wp_dropdown_cats($category[0]->parent);
		$this->load->helper('form');
		
		$tableCategoryHeader=array(0=>null,'name'=>'Name','link'=>'url','option'=>'Option','count'=>'Post');
		self::listTableCategory();
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'option_category'=>$option_category,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'CategoryListOption'=>$list_option,
					'category'=>$category[0]
				);
		
		$this->view(
				array('admin-header',
						'category-edit',
						'admin-footer'), 
				$data);
	}
	
	
	public function checkURL(){
		if(isset($_POST) and isset($_POST['url'])){
			if($url=trim($_POST['url'])){
				
				if($this->model('category_models_index')->check_url($url))
					echo '<div class="alert"><button type="button" class="close" data-dismiss="alert">&times;</button>
							Il existe '.$url.'</div>';
			}
		}
	}
	public function listTableCategory( $currentcat = 0,  $parent = 0, $level = 0,$filter=''){
		$categories = $this->model('category_models_index')->get_categories( $parent,$filter );
		
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$current= ( $currentcat == $cat->id )?1:0;
				
				$catRow=array("id"=>$cat->id,
										"parent"=>$cat->parent,
										"name"=>"$pad{$cat->name}",
										"short-name"=>$cat->name,
										"slug"=>$cat->slug,
										"link"=>$cat->link,
										"option"=>$cat->option,
										"description"=>$cat->description,
										"order"=>$cat->order,
										"active"=>$cat->active,
										"count"=>$cat->count,
										"current"=>$current);
				if($cat->option)$catRow['option']=$this->category_models_index->get_option($cat->option);
				$this->category[$cat->id]=$catRow;
				self::listTableCategory( $currentcat, $cat->id, $level +1,$filter );
			}
		}
	
	}
	public function wp_dropdown_cats( $currentcat = 0,  $parent = 0, $level = 0, $category = '' ) {
		
		if(!isset($this->category_models_index))$this->model('category_models_index');
		
		$categories = $this->category_models_index->get_categories( $parent );
		
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
					$pad = str_repeat( '&#8211; ', $level );
					$s= ( $currentcat == $cat->id )?" selected='selected'":'';
					$catOption=  "<option value='{$cat->id}' $s >$pad{$cat->name}</option>\n\t";
					$category .=self::wp_dropdown_cats( $currentcat, $cat->id, $level +1, $catOption );
			}
		} 
		return $category;
	}

	public function dropdown_option_cats( $currentcat = 0,  $parent = 0, $level = 0, $category = '' ) {
		$categories = $this->model('category_models_index')->get_categories( $parent );
	
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$s= ( $currentcat == $cat->id )?" selected='selected'":'';
				$catOption=  "<option value='{$cat->id}' $s >$pad{$cat->name}</option>\n\t";
				$category .=self::dropdown_option_cats( $currentcat, $cat->id, $level +1, $catOption );
			}
		}
		return $category;
	}

	public function check_list_cats( $currentcat = array(),  $parent = 0, $level = 0, $category = '' ) {
		$categories = $this->model('category_models_index')->get_categories( $parent );
		$catOption='';
		if ( is_array($categories) and !empty($categories) ) {
			if($level)$catOption.=  "\n<ul class='children'>\n";
			foreach ( $categories as $cat ) {
				//$pad = str_repeat( '&#8211; ', $level );
				$s= ( in_array($cat->id, $currentcat) )?" checked":'';
				$catOption.=  "
				<li class='popular-category' id='category-{$cat->id}'>
					<label class='selectit'>";
				$sub_cat=$this->category_models_index->get_categories(  $cat->id );
				if(count($sub_cat)==0)
					$catOption.=  "<input value='{$cat->id}' type='checkbox' name='category[]' id='in-category-{$cat->id}' $s />";
				$catOption.=  " {$cat->name}</label>";
				$catOption .=self::check_list_cats( $currentcat, $cat->id, $level +1, $category );
				$catOption .=  "
				</li>";
			}
	
			if($level)$catOption.=  "\n</ul>\n";
		}
			return $catOption;
	}

	public function check_list_top_cats() {
		$categories = $this->model('category_models_index')->get_top_categories();
		$catOption='';
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$catOption.=  "
				<li class='popular-category' id='category-{$cat->id}'>
				<label class='selectit'><input value='{$cat->id}' type='checkbox' name='post_category[]' id='in-category-{$cat->id}' /> {$cat->name}</label>
				</li>";
			}
		}
			return $catOption;
	}
	public function get_link_category($id_category){
		return $this->model('category_models_index')->get_link_category($id_category);
	}
	public function get_name_category($id_category){
		return $this->model('category_models_index')->get_name_category($id_category);
	}
	public function get_breadcrumb_category($id_category){
		return $this->model('category_models_index')->get_all_category_by_id($id_category);
	}
	public function get_html_category($id_category){
		$child=$html='';
		$Breadcrumb=self::get_breadcrumb_category($id_category);
		if($Breadcrumb){
			if(isset($Breadcrumb['child'])){
			$child='
					  <span itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
					    <a href="'.$Breadcrumb['child']->link.'" itemprop="url"><span itemprop="title">'.$Breadcrumb['child']->name.'</span> </a> 
					  </span>';
			}
			if(isset($Breadcrumb['parent'])){
				$html='
				<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
						<a href="'.$Breadcrumb['parent']->link.'" itemprop="url"><span itemprop="title">'.$Breadcrumb['parent']->name.'</span></a> ›'.$child.'
				</span>
				';
			}
		}
			
		return $html;
		
	}

	public function down($id){
		$copyId=array();
		$ids=$this->model('category_models_index')->listIdShameParent($id);
		foreach($ids as $index=>$orderId){
			if($orderId==$id){
				$down=$orderId;
				continue;
			}
			$copyId[]=$orderId;
			if(isset($down)){
				$copyId[]=$down;
				unset($down);
			}
		}
		foreach($copyId as $order=>$id)
			$OrderArray[$id]=array('order'=>$order);
		$this->model('category_models_index')->updateOrder($OrderArray);
		$this->load->helper('url');
		redirect('category/admin');
	}
	
	public function up($id){
		$copyId=array();
		$ids=$this->model('category_models_index')->listIdShameParent($id);
		foreach($ids as $index=>$orderId){
			if($orderId==$id){
				$up=array_pop($copyId);
			}
			$copyId[]=$orderId;
			if(isset($up)){
				$copyId[]=$up;
				unset($up);
			}
		}
		foreach($copyId as $order=>$id)
			$OrderArray[$id]=array('order'=>$order);
		$this->model('category_models_index')->updateOrder($OrderArray);
	
		$this->load->helper('url');
		redirect('category/admin');
	}
}